<template>
  <div class="app-container">
    <div class="search-area" v-show="showSearch">
      <el-form
        :model="queryParams"
        @submit.native.prevent
        ref="queryForm"
        size="small"
        :inline="true"
        class="clearfix"
        label-width="auto"
      >
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input
            v-model="queryParams.supplierName"
            placeholder="请输入供应商名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="供应商类型" prop="supplierTypeList">
          <el-select
            v-model="queryParams.supplierTypeList"
            multiple
            collapse-tags
            placeholder="请选择供应商类型"
            clearable
          >
            <el-option
              v-for="dict in dict.type.supplier_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="main-content">
      <el-row :gutter="10" class="mb24">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['biz:supplier:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['biz:supplier:remove']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button plain size="mini" @click="downloadTemplate()" v-hasPermi="['biz:supplier:import']"
            >下载导入模板</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button plain icon="el-icon-plus" size="mini" @click="importData()" v-hasPermi="['biz:supplier:import']"
            >导入</el-button
          >
          <input
            type="file"
            @change="onFileChange"
            ref="uploadFileInput"
            enctype="multipart/form-data"
            style="display: none"
            accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
          />
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="供应商名称" align="center" prop="supplierName" v-if="columns.supplierName.visible" fixed/>
        <el-table-column label="供应商类型" align="center" prop="supplierTypeName" v-if="columns.supplierType.visible">
        </el-table-column>
        <el-table-column label="编号" align="center" prop="supplierNumber" v-if="columns.supplierNumber.visible" />
        <el-table-column label="规模" align="center" v-if="columns.scale.visible">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.supplier_scale" :value="scope.row.scale" />
          </template>
        </el-table-column>
        <el-table-column label="标签" align="center" v-if="columns.scale.visible">
          <template slot-scope="scope">
            <span v-for="(tag, index) in scope.row.supplierTagList">{{
              `${index !== 0 ? '、' : ''}${tag.tagTypeName}`
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="联系人" align="center" prop="contactPerson" v-if="columns.contactPerson.visible" />
        <el-table-column label="联系方式" align="center" prop="contactNumber" v-if="columns.contactNumber.visible" />
        <!-- <el-table-column label="附件列表" align="center" prop="annexList" v-if="columns.annexList.visible" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  width="180"  fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-tickets"
              @click="handleUpdate(scope.row, true)"
              v-hasPermi="['biz:supplier:getInfo']"
              >查看</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['biz:supplier:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['biz:supplier:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <details-pop
      :show="showDetails"
      :disabled="disabled"
      :id="supplierId"
      @refresh="getList"
      @cancel="showDetails = false"
    ></details-pop>
  </div>
</template>

<script>
import { listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier, importSupplier } from '@/api/biz/supplier'
import DetailsPop from './SupplierDetails.vue'
export default {
  name: 'Supplier',
  dicts: ['supplier_type', 'supplier_tag', 'supplier_scale'],
  components: {
    DetailsPop,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商表格数据
      supplierList: [],
      // 弹出层标题
      title: '',

      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierName: null,
        supplierTypeList: null,
      },
      // 表单参数
      form: {},
      // 列信息
      columns: {
        supplierName: { label: '供应商名称', visible: true },
        supplierType: { label: '供应商类型', visible: true },
        supplierNumber: { label: '编号', visible: true },
        scale: { label: '规模', visible: true },
        contactPerson: { label: '联系人', visible: true },
        contactNumber: { label: '联系方式', visible: true },
        annexList: { label: '附件列表', visible: true },
      },
      showDetails: false,
      supplierId: null,
      disabled: false, // 是否禁止表单填写
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询供应商列表 */
    getList() {
      this.loading = true
      listSupplier(this.queryParams).then((response) => {
        this.supplierList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.disabled = false
      this.supplierId = null
      this.showDetails = true
    },
    /** 修改按钮操作 */
    handleUpdate(row, readOnly) {
      this.supplierId = row.id
      this.disabled = readOnly
      this.showDetails = true
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除供应商编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSupplier(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'biz/supplier/export',
        {
          ...this.queryParams,
        },
        `supplier_${new Date().getTime()}.xlsx`
      )
    },
    onFileChange() {
      const formData = new FormData()
      formData.append('file', this.$refs.uploadFileInput.files[0])
      this.loading = true
      importSupplier(formData)
        .then((response) => {
          this.loading = false
          this.$modal.msgSuccess('导入成功')
          // 清除文件内容，避免下次不生效
          this.$refs.uploadFileInput.value = ''
          this.resetQuery()
        })
        .catch((err) => {
          this.loading = false
          // 清除文件内容，避免下次不生效
          this.$refs.uploadFileInput.value = ''
        })
    },
    importData() {
      this.$refs.uploadFileInput.click()
    },
    downloadTemplate() {
      this.loading = true
      // 使用相对路径
      const fileUrl = '/supplierTemplate.xlsx'
      // 创建一个隐藏的a标签
      const link = document.createElement('a')
      link.href = fileUrl
      link.download = '供应商模板.xlsx' // 设置下载文件名
      // 将a标签添加到DOM并触发点击事件
      document.body.appendChild(link)
      link.click()
      // 移除a标签
      document.body.removeChild(link)
      this.loading = false
      this.$modal.msgSuccess('下载成功')
    },
  },
}
</script>
<style scoped lang="scss">
.el-date-editor,
.el-select {
  width: 100%;
}
</style>
